﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace Dk {
    /// 注意点 ：
    /// 选中行为在所有取消选中行为之后激活
    /// 

    [RequireComponent (typeof (Button))]
    public class BaseDkBtn : DkMono {
        [HideInInspector]
        protected Button btn;
        protected string SelectEventName = EventName.NavgationBarBtnSelect;
        protected string UnSelectEventName = EventName.NavgationBarBtnUnSelect;
        protected System.Action action;

        protected System.Action unSelectAct;

        public string indentifiName = null;

        //可外部定义行为---偏向用于按钮/自身状态改变/动画表现；
        //按钮事件依旧通过 action来实现
        public IbtnAnim[] btnAnim = null;

        [SerializeField]
        public bool isSelect = false;
        protected Listener selectHandler;
        protected Listener unSelectHandler;

        private void Awake () {
            OnAwake ();
        }
        public override void OnAwake () {

            btn = GetComponent<Button> ();
            selectHandler = new Listener (NotifySelect, this.gameObject);
            unSelectHandler = new Listener(NotifyUnSelect, this.gameObject);

        }
        public override void OnStart()
        {
            btn.onClick.AddListener(NotifyBtn);

            if (btnAnim == null || btnAnim.Length <= 0) btnAnim = this.GetComponents<IbtnAnim>();
            if (btnAnim == null || btnAnim.Length <= 0) { btnAnim = new IbtnAnim[] { this.gameObject.AddComponent<NormalSelect>() }; }

            BaseEventManager.Instance.AddListener(SelectEventName, selectHandler);
            BaseEventManager.Instance.AddListener(UnSelectEventName, unSelectHandler);

            //激活初始动画
            if (isSelect)
                foreach (var i in btnAnim) { i.SelectAct(this); }

            else
                foreach (var i in btnAnim) { i.UnSelectAct(this); }
        }

        protected virtual void Init() { }

        /// <summary>
        /// 按钮事件绑定
        /// </summary>
        /// <param name="act"></param>
        public void AddEvent(System.Action act)
        {
            action += act;
        }

        public void AddUnSelectAct (System.Action act) {
            unSelectAct += act;
        }

        /// <summary>
        /// 当按钮被点击 进行事件发布
        /// </summary>
        public void NotifyBtn () {

            Debug.Log("fabu");
            string currstr = string.IsNullOrEmpty (indentifiName) ? this.gameObject.GetHashCode ().ToString () : indentifiName;

            BaseEventManager.Instance.Notify (UnSelectEventName, new EventMsg () { msg = currstr });
            BaseEventManager.Instance.Notify (SelectEventName, new EventMsg () { msg = currstr });

        }

        // 定义不可多次点击的按钮
        protected void SelectThis () {

            if (isSelect) return;
            isSelect = true;
            foreach (var i in btnAnim) i.SelectAct (this);
            action?.Invoke ();

        }

        protected void UnSelectThis () {

            if (!isSelect) return;
            isSelect = false;
            foreach (var i in btnAnim) i.UnSelectAct (this);
            unSelectAct?.Invoke ();

        }


        //通过事件发放进行触发并且在触发后退出事件继续执行兵相应Unselect方法
        protected void NotifySelect(EventMsg msg)
        {
            string currstr = string.IsNullOrEmpty (indentifiName) ? this.gameObject.GetHashCode ().ToString () : indentifiName;

            if (msg.msg == currstr)
            {
                SelectThis();
                return;
            }
            
        }

        protected void NotifyUnSelect(EventMsg msg)
        {

            string currstr = string.IsNullOrEmpty (indentifiName) ? this.gameObject.GetHashCode ().ToString () : indentifiName;
            if (msg.msg != currstr)
            {
                UnSelectThis();
            }
        }

    }

    public class NormalSelect : IbtnAnim {
        public override void SelectAct (BaseDkBtn o) {
        }

        public override void UnSelectAct (BaseDkBtn o) {
        }
    }

    public abstract class IbtnAnim : MonoBehaviour {

        [SerializeField]
        protected bool isSelect = false;

        void Start () {
            if (isSelect) SelectAct (null);
        }

        public abstract void SelectAct (BaseDkBtn o);
        public abstract void UnSelectAct (BaseDkBtn o);
    }

}